package sample;

import net.coobird.thumbnailator.Thumbnails;
import utils.fileHandler.FileHandler;
import utils.utilTime.UtilTime;
import utils.wallpaperHandler.WallpaperHandler;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.List;

import static utils.grabberFFmpegImage.GrabberFFmpegImage.randomGrabberFFmpegImage;

public class WallpaperChecker {
    public static void checkWallpaper() {
        String preImageDirAbsolutePath = FileHandler.getPreImageDirAbsolutePath();
        String hDImageDirAbsolutePath = FileHandler.gethDImageDirAbsolutePath();
        File preFile = new File(preImageDirAbsolutePath);
        File hdFile = new File(hDImageDirAbsolutePath);
        String[] preList = preFile.list();
        String[] comList = hdFile.list();
        assert preList != null;
        HashSet<String> strings = new HashSet<>(List.of(preList));
        assert comList != null;
        for (String s : comList) {
            if (!strings.contains(s)) {
                File fromPic = new File(hDImageDirAbsolutePath + File.separator + s);
                File toPic = new File(preImageDirAbsolutePath + File.separator + s);
                try {
                    Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
                } catch (IOException e) {
                    System.out.println("Error: create :"+ FileHandler.getPreImageDirName() + toPic.getPath() + "from :" + fromPic.getPath() + " fail " + UtilTime.getStringTimeNow());
                    e.printStackTrace();
                }
            }
            WallpaperHandler.addWallpaper(preImageDirAbsolutePath + File.separator + s,hDImageDirAbsolutePath + File.separator + s, WallpaperHandler.preImageType);
        }
        WallpaperHandler.save();
    }

    public static void checkLiveWallpaper() {
        String preVideoDirAbsolutePath = FileHandler.getPreVideoDirAbsolutePath();
        String hDVideoDirAbsolutePath = FileHandler.gethDVideoDirAbsolutePath();
        File preFile = new File(preVideoDirAbsolutePath);
        File completeFile = new File(hDVideoDirAbsolutePath);
        String[] preList = preFile.list();
        String[] comList = completeFile.list();
        assert preList != null;
        HashSet<String> strings = new HashSet<>();
        for (String value : preList) {
            strings.add(FileHandler.getNameWithoutType(value));
        }
        assert comList != null;
        for (String s : comList) {
            String nameWithoutType = FileHandler.getNameWithoutType(s);
            if (!strings.contains(nameWithoutType)) {
                String toVideoImageUrl = preVideoDirAbsolutePath + File.separator + nameWithoutType + ".jpg";
                String fromVideoUrl = hDVideoDirAbsolutePath + File.separator + s;
                try {
                    randomGrabberFFmpegImage(fromVideoUrl, preVideoDirAbsolutePath, nameWithoutType);
                } catch (IOException e) {
                    System.out.println("Error: create :"+ FileHandler.getPreVideoDirName() + toVideoImageUrl + "from :" + fromVideoUrl + " fail" + UtilTime.getStringTimeNow());
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            WallpaperHandler.addWallpaper(preVideoDirAbsolutePath + File.separator + nameWithoutType + ".jpg",hDVideoDirAbsolutePath + File.separator + s, WallpaperHandler.preVideoType);
        }
        WallpaperHandler.save();
    }

    public static String createPreImage(String hDIVUrl) {
        String preIUrl = null;
        if (hDIVUrl.contains(FileHandler.gethDImageDirName())) {
            if (!FileHandler.getFileType(hDIVUrl).equals("gif")) {
                File fromPic = new File(hDIVUrl);
                File toPic = new File(hDIVUrl.replace(FileHandler.gethDImageDirName(), FileHandler.getPreImageDirName()));
                try {
                    Files.copy(fromPic.toPath(),toPic.toPath());
                    WallpaperHandler.addWallpaper(toPic.getPath(),fromPic.getPath(), WallpaperHandler.hDImageType);
                    preIUrl= toPic.getPath();
                } catch (IOException e) {
                    System.out.println("Error: create :" + FileHandler.getPreVideoDirName() + toPic.getPath() + "from :" + fromPic.getPath() + " fail");
                    e.printStackTrace();
                }
            }
        } else {
            String toVideoImageUrl = hDIVUrl.replace(FileHandler.gethDVideoDirName(), FileHandler.getPreVideoDirName());
            File toVideoImage = new File(toVideoImageUrl);
            try {
                preIUrl = randomGrabberFFmpegImage(hDIVUrl, toVideoImage.getParent(), FileHandler.getNameWithoutType(toVideoImage.getName()));
                WallpaperHandler.addWallpaper(preIUrl,hDIVUrl, WallpaperHandler.preVideoType);
            } catch (Exception e) {
                System.out.println("Error: create :" + FileHandler.getPreVideoDirName() + toVideoImageUrl + "from :" + hDIVUrl + " fail");
                e.printStackTrace();
            }
        }
        return preIUrl;
    }
}
